﻿using Autodesk.AutoCAD.Geometry;
using System.Collections.Generic;
using System;
using System.Linq;
using Galaxy.AutoCAD.Doubles;

namespace Galaxy.AutoCAD.Geometrys
{
    public static class Point3dTool
    {
        #region 获得三维点集的中心点
        public static Point3d GetCentrePoint(List<Point3d> pt3ds)
        {
            double x = 0.0;
            double y = 0.0;
            double z = 0.0;
            if (pt3ds.Count == 0 || pt3ds.Count == 1)
            {
                throw new ArgumentOutOfRangeException("传入的参数三维点集有误");
            }
            else
            {
                foreach (Point3d pt3d in pt3ds)
                {
                    x += pt3d.X;
                    y += pt3d.Y;
                    z += pt3d.Z;
                }
                return new Point3d(x / pt3ds.Count, y / pt3ds.Count, z / pt3ds.Count);
            }
        }
        public static Point3d GetCentrePoint(params Point3d[] pt3ds)
        {
            return GetCentrePoint(pt3ds.ToList());
        }
        #endregion
        #region 计算从第一点到第二点所确定的矢量与X轴方向的夹角
        public static double AngleFromXAxis(this Point3d pt1, Point3d pt2)
        {
            //构建一个从第一点到第二点所确定的矢量
            Vector2d vector = new Vector2d(pt1.X - pt2.X, pt1.Y - pt2.Y);
            //返回该矢量和X轴正半轴的角度（弧度）
            return vector.Angle;
        }
        #endregion
        #region 判断点集是否在同一个水平面上
        public static bool IsSameHorizontalPlane(List<Point3d> pt3ds)
        {
            bool result = true;
            foreach (Point3d pt3d in pt3ds)
            {
                if (pt3d.Z != pt3ds[0].Z)
                {
                    result = false;
                }
            }
            return result;
        }
        #endregion
        #region 获得点集中的点X值最大的点
        public static Point3d GetPoint3dOfMaximumX(List<Point3d> pt3ds,out int index)
        {
            Point3d pt3d = pt3ds[0];
            foreach (Point3d outPoint3d in pt3ds)
            {
                if (outPoint3d.X > pt3d.X)
                {
                    pt3d = outPoint3d;
                }
            }
            index = pt3ds.IndexOf(pt3d);
            return pt3d;
        }
        #endregion
    }
}
